package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.BiologyCategory;
import com.jgsu.farm.entity.Enemy;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.BiologyCategoryMapper;
import com.jgsu.farm.mapper.EnemyMapper;
import com.jgsu.farm.mapper.sql.EnemyMapperSql;
import com.jgsu.farm.service.EnemyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class EnemyServiceImpl implements EnemyService {
    @Autowired
    BiologyCategoryMapper biologyCategoryMapper;
    @Autowired
    EnemyMapper enemyMapper;
    @Autowired
    EnemyMapperSql enemyMapperSql;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加
     */
    @Override
    public void add(Enemy enemy) {
        // 先从获取的category与进行查询category表，查询成功才能插入语
        BiologyCategory biologyCategory = biologyCategoryMapper.selectByPrimaryKey(enemy.getCategory());
        if (biologyCategory != null) {
            enemyMapper.insert(enemy);
        } else {
            throw new DataAddException("604", "插入的category在category表中没有，请先插入到category表");
        }
    }

    /**
     * 删除
     */
    @Override
    public void delete(Integer enemyid) {
        Integer id = enemyMapper.deleteByPrimaryKey(enemyid);
        if (id == 0 || id == null) {
            throw new DataNotExistException("603", "不存在该id作物删除失败");
        }
    }

    /**
     * 分页查询
     */
    @Override
    public PageInfo<Enemy> findList(int page, int pagesize) {
        PageHelper.startPage(page, pagesize);
        List<Enemy> enemies = (List<Enemy>) redisTemplate.opsForValue().get("enemies");
        if (null == enemies) {
            synchronized (this) {
                enemies = (List<Enemy>) redisTemplate.opsForValue().get("enemies");
                if (null == enemies) {
                    enemies = enemyMapperSql.selectAll();
                    log.info("从数据库中查询enemies。。。。");
                    redisTemplate.opsForValue().set("enemies", enemies);
                    log.info("将从数据库中查询的enemies数据存入redis。。。。");
                }
            }
        }
        PageInfo<Enemy> pageInfoEnemies = new PageInfo<Enemy>(enemies);
        return pageInfoEnemies;
    }

    /**
     * 更新
     */
    @Override
    public void update(Enemy enemy) {
        // 1. 先查询数据库中是否存在要查询的数据
        Enemy id = enemyMapper.selectByPrimaryKey(enemy.getEnemyid());
        BiologyCategory biologyCategory = biologyCategoryMapper.selectByPrimaryKey(enemy.getCategory());
        if (id != null && biologyCategory != null) {
            // 2. 如果存在，则更新数据
            enemyMapper.updateByPrimaryKey(enemy);
        } else {
            // 3. 如果不存在，则抛异常
            throw new DataNotExistException("603", "修改天敌信息时，没有该天敌分类请先在分类表中更新");
        }

    }
}
